package net.spunofbook;

import java.util.List;

import net.spunofbook.modelo.VendaRepositorio;
import net.spunofbook.negocio.Venda;
import net.spunofbook.negocio.VendaAdapter;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

public class TelaVenda extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tela_venda);
		
		VendaRepositorio tabelaVenda = new VendaRepositorio(this);		
		List<Venda> vendas = tabelaVenda.buscarTodasAsVendas(); 
		
		ListView listaDeVendas = (ListView) findViewById(R.id.telaVenda_listaDeVendas);
		listaDeVendas.setAdapter(new VendaAdapter(this, vendas));
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		getMenuInflater().inflate(R.menu.tela_venda, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		Context v = getBaseContext();
		switch (item.getItemId()) {
			case R.id.telaVendaMenu_cadastrar:
				chamarTelaCadastro(v);
				break;
		
			case R.id.telaVendaMenu_inicio:
				chamarTelaInicio(v);
				break;
		
			default:
				break;
		}
		
		return true;
	}
	
	
	private void chamarTelaInicio(Context context) {
		startActivity(new Intent(context, TelaPrincipal.class));
		finish();
	}
	
	private void chamarTelaCadastro(Context context){
		startActivity(new Intent(context, TelaVendaCadastro.class));
		finish();
	}
	
}
